/*
 *  Copyright (C) 2015 The yuhaiyang Android Source Project
 *
 *  Licensed under the yuhaiyang License, (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       https://coding.net/yuhaiyang/License.git
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  @author: y.haiyang@qq.com
 *
 */

package com.yuhaiyang.xmlparser;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.yuhaiyang.utils.Utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class XmlParserActivity extends Activity implements View.OnClickListener{
    //private static final String TAG = XmlParserActivity.class.getSimpleName();
    private static final String TAG = "nian";
    private static final String APP_PATH = "/XmlParser";
    private static final String SAX_FILE_NAME = "sax_books.xml";
    private static final String DOM_FILE_NAME = "dom_books.xml";
    private static final String PULL_FILE_NAME = "pull_books.xml";

    private Button mParserXml;
    private Button mWriteXml;
    private TextView mShowTime;
    private RadioGroup mParserStyles;

    private List<Book> mBooks = new ArrayList<Book>();
    private IBookParser mBookParser;
    private String mFileName = SAX_FILE_NAME;

    private String mTipString;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        // Set Default Parser is SaxParser
        mBookParser = new SaxBookParser();
        mTipString = getString(R.string.tip_sax);
        mTipString = Utils.plusString(mTipString, Utils.NEW_LINE);
    }

    private void init(){
        mShowTime = (TextView) findViewById(R.id.showtime);

        mParserXml = (Button) findViewById(R.id.parser_xml);
        mParserXml.setOnClickListener(this);
        mWriteXml = (Button) findViewById(R.id.write_xml);
        mWriteXml.setOnClickListener(this);;

        mParserStyles = (RadioGroup) findViewById(R.id.styles);
        mParserStyles.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                switch (i){
                    case R.id.sax_style:
                        mBookParser = new SaxBookParser();
                        mFileName = SAX_FILE_NAME;
                        mTipString = getString(R.string.tip_sax);
                        break;
                    case R.id.dom_style:
                        mBookParser = new DomBookParser();
                        mFileName = DOM_FILE_NAME;
                        mTipString = getString(R.string.tip_dom);
                        break;
                    case R.id.pull_style:
                        mBookParser = new PullBookParser();
                        mFileName = PULL_FILE_NAME;
                        mTipString = getString(R.string.tip_pull);
                        break;
                }
                mTipString = Utils.plusString(mTipString, Utils.NEW_LINE);
            }
        });

    }

    @Override
    public void onClick(View v) {
        //mBooks.clear();
        switch (v.getId()){
            case R.id.parser_xml:
                try {
                    InputStream input = getAssets().open("books.xml");
                    mBooks = mBookParser.parse(input);
                } catch (Exception e) {
                    String error = Utils.plusString(mTipString, e.toString());
                    mShowTime.setText(error);
                }
                String showtime = mTipString;
                for (Book book : mBooks) {
                    showtime = Utils.plusString(showtime,"*************************", Utils.NEW_LINE);
                    showtime = Utils.plusString(showtime, book.toString(), Utils.NEW_LINE);
                }
                if(!mBooks.isEmpty()) {
                    mShowTime.setText(showtime);
                }
                break;
            case R.id.write_xml:
                try {
                    String xml = mBookParser.write(mBooks);

                    File storage = Environment.getExternalStorageDirectory();
                    File path = new File(Utils.plusString(storage.getPath() , APP_PATH));
                    if(!path.exists()){
                        path.mkdirs();
                    }
                    File file = new File(Utils.plusString(path.getPath(), "/" , mFileName));
                    if(!file.exists()){
                        file.createNewFile();
                    }
                    FileOutputStream stream = new FileOutputStream(file);
                    stream.write(xml.getBytes("UTF-8"));
                    stream.close();
                    Toast.makeText(XmlParserActivity.this,mFileName + " saved into " + path.getPath(),Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
                break;
        }
    }
}
